<template>
      <div>
            <div style="margin-top: 15px;">
                  当前密码 <el-input v-model="oldPassword"></el-input>
            </div>
            <div style="margin-top: 15px;">
                  新密码 <el-input v-model="newPassword"></el-input>
            </div>
            <div style="margin-top: 15px;">
                  确认密码 <el-input v-model="confirmPassword"></el-input>
            </div>
            <div style="margin-top: 15px;">
                  <el-button type="success" @click="updatePassword">确定修改</el-button>
            </div>
      </div>
</template>
<script>
export default {
      name:"updatePassword",
      props:["user"],
      data() {
            return {
                  oldPassword: "",
                  newPassword: "",
                  confirmPassword: "",
            }
      },
      created() {
            console.log(this.user);
      },
      methods: {
            updatePassword() {
                  if(this.oldPassword==""){
                        this.$message.error("当前密码不能为空");
                        return;
                  }
                  if(this.newPassword==""){
                        this.$message.error("新密码不能为空");
                        return;
                  }
                  if(this.confirmPassword==""){
                        this.$message.error("确认密码不能为空");
                        return;
                  }
                  if(this.newPassword!=this.confirmPassword){
                        this.$message.error("新密码与确认密码不一致");
                        return;
                  }
                  if(this.oldPassword!=this.user.password){
                        this.$message.error("密码不正确");
                        return;
                  }
                  
                  return this.axios.post("http://localhost:9000/user/user/updatePassword",{
                        userid:this.user.id,
                        password:this.newPassword,
                  })
                  .then(res => {
                        console.log(res);
                        this.$message.success("修改密码成功");
                        this.user.password = this.newPassword;
                        var temp=JSON.parse(localStorage.getItem("user"));
                        temp.content=this.user;
                        localStorage.setItem("user",JSON.stringify(temp));
                        // location.reload();
                  })
                  .catch(err => {
                        console.log(err);
                        this.$message.error("密码修改失败");
                  })
            },
      }
};

</script>
<style scoped>

</style>